package org.hcyspartnoc.demo.service.course.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hcyspartnoc.demo.common.core.context.BaseContext;
import org.hcyspartnoc.demo.common.core.context.BaseContextHolder;
import org.hcyspartnoc.demo.mapper.college.CollegeMapper;
import org.hcyspartnoc.demo.mapper.course.CouRelationMapper;
import org.hcyspartnoc.demo.mapper.course.CouTeamMapper;
import org.hcyspartnoc.demo.mapper.course.CouTeamMebMapper;
import org.hcyspartnoc.demo.mapper.course.CourseMapper;
import org.hcyspartnoc.demo.mapper.major.MajorMapper;
import org.hcyspartnoc.demo.mapper.system.SysRoleMapper;
import org.hcyspartnoc.demo.mapper.system.SysUserMapper;
import org.hcyspartnoc.demo.pojo.dto.course.CourseInfoDto;
import org.hcyspartnoc.demo.pojo.po.college.CollegePo;
import org.hcyspartnoc.demo.pojo.po.course.CouRelationPo;
import org.hcyspartnoc.demo.pojo.po.course.CouTeamMebPo;
import org.hcyspartnoc.demo.pojo.po.course.CouTeamPo;
import org.hcyspartnoc.demo.pojo.po.course.CoursePo;
import org.hcyspartnoc.demo.pojo.po.major.MajorPo;
import org.hcyspartnoc.demo.pojo.po.system.SysRolePo;
import org.hcyspartnoc.demo.pojo.po.system.SysUserPo;
import org.hcyspartnoc.demo.pojo.vo.course.*;
import org.hcyspartnoc.demo.pojo.vo.system.SysRoleVo;
import org.hcyspartnoc.demo.service.course.ICourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CoursePo> implements ICourseService {
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private CouTeamMebMapper couTeamMebMapper;

    @Resource
    private CouTeamMapper couTeamMapper;

    @Resource
    private MajorMapper majorMapper;

    @Resource
    private CollegeMapper collegeMapper;

    @Resource
    private CouRelationMapper couRelationMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Override
    @Transactional
    public List<CourseListVo> getCourseListByUserID() {
        // 通过上下文获取当前用户
        SysUserPo currentUser = BaseContextHolder.getContext().getBaseCurrentUser();
        if (currentUser == null) {
            // 如果用户不存在，抛出异常
            throw new RuntimeException("用户不存在");
        }
        SysRolePo sysRolePo = sysRoleMapper.selectById(currentUser.getRoleId());
        if(sysRolePo == null){
            throw new RuntimeException("用户权限错误");
        }
        List<CourseListVo> courseVoList = new ArrayList<>();
        if(sysRolePo.getId() == 100000004){// 普通教师
            List<CouTeamMebPo> couTeamMebPoList = couTeamMebMapper.selectList(
                    new LambdaQueryWrapper<CouTeamMebPo>().eq(CouTeamMebPo::getMemberID, currentUser.getId()));
            for (CouTeamMebPo couTeamMebPo : couTeamMebPoList) {
                List<CoursePo> coursePoList = courseMapper.selectList(
                        new LambdaQueryWrapper<CoursePo>()
                                .select(CoursePo::getId, CoursePo::getCourseCode, CoursePo::getCourseName)
                                .eq(CoursePo::getCouTeamId, couTeamMebPo.getTeamID()) // 按团队 ID 查询
                );
                for(CoursePo coursePo : coursePoList){
                    CourseListVo courseListVo = new CourseListVo();
                    courseListVo.setId(coursePo.getId());
                    courseListVo.setName(coursePo.getCourseCode() + "-" + coursePo.getCourseName());
                    courseVoList.add(courseListVo);
                }
            }

        }else if(sysRolePo.getId() == 100000003){// 课程负责人
            List<CouTeamPo> couTeamPoList = couTeamMapper.selectList(
                    new LambdaQueryWrapper<CouTeamPo>().eq(CouTeamPo::getDirectorID, currentUser.getId()));
            for (CouTeamPo couTeamPo : couTeamPoList) {
                List<CoursePo> coursePoList = courseMapper.selectList(
                        new LambdaQueryWrapper<CoursePo>()
                                .select(CoursePo::getId, CoursePo::getCourseCode, CoursePo::getCourseName)
                                .eq(CoursePo::getId, couTeamPo.getCourseID()) // 按 课程 ID 查询
                );
                for(CoursePo coursePo : coursePoList){
                    CourseListVo courseListVo = new CourseListVo();
                    courseListVo.setId(coursePo.getId());
                    courseListVo.setName(coursePo.getCourseCode() + "-" + coursePo.getCourseName());
                    courseVoList.add(courseListVo);
                }
            }
        }else if(sysRolePo.getId() == 100000002){// 专业负责人
            List<MajorPo> majorPos = majorMapper.selectList(
                    new LambdaQueryWrapper<MajorPo>()
                            .select(MajorPo::getId)
                            .eq(MajorPo::getMajorManId,currentUser.getId())
            );
            for (MajorPo majorPo : majorPos){
                List<CoursePo> coursePoList = courseMapper.selectList(
                        new LambdaQueryWrapper<CoursePo>()
                                .select(CoursePo::getId, CoursePo::getCourseCode, CoursePo::getCourseName)
                                .eq(CoursePo::getMajorId, majorPo.getId()) // 按适用专业 ID 查询
                );
                for(CoursePo coursePo : coursePoList){
                    CourseListVo courseListVo = new CourseListVo();
                    courseListVo.setId(coursePo.getId());
                    courseListVo.setName(coursePo.getCourseCode() + "-" + coursePo.getCourseName());
                    courseVoList.add(courseListVo);
                }
            }
        }else if(sysRolePo.getId() == 100000001){// 教学负责人
            CollegePo collegePo = collegeMapper.selectOne(
                    new LambdaQueryWrapper<CollegePo>()
                            .select(CollegePo::getId)
                            .eq(CollegePo::getCollegeManagerID,currentUser.getId())
            );
            List<CoursePo> coursePoList = courseMapper.selectList(
                    new LambdaQueryWrapper<CoursePo>()
                            .select(CoursePo::getId, CoursePo::getCourseCode, CoursePo::getCourseName)
                            .eq(CoursePo::getCollegeId, collegePo.getId()) // 按开设学院 ID 查询
            );
            for(CoursePo coursePo : coursePoList){
                CourseListVo courseListVo = new CourseListVo();
                courseListVo.setId(coursePo.getId());
                courseListVo.setName(coursePo.getCourseCode() + "-" + coursePo.getCourseName());
                courseVoList.add(courseListVo);
            }
        }else{// 管理员
            List<CoursePo> coursePoList = courseMapper.selectList(
                    new LambdaQueryWrapper<CoursePo>()
                            .select(CoursePo::getId, CoursePo::getCourseCode, CoursePo::getCourseName)
            );
            for(CoursePo coursePo : coursePoList){
                CourseListVo courseListVo = new CourseListVo();
                courseListVo.setId(coursePo.getId());
                courseListVo.setName(coursePo.getCourseCode() + "-" + coursePo.getCourseName());
                courseVoList.add(courseListVo);
            }
        }
        return courseVoList;
    }

    @Override
    public CourseVo getCourseInfoByID(Integer courseId) {
        CourseVo courseVo = courseMapper.getCourseInfoByID(courseId);
        CourseTeamVo courseTeamVo = couTeamMapper.getCourseTeamInfoByCourseId(courseId);
        courseVo.setDirector(courseTeamVo);
        List<CouTeamMebVo> couTeamMebVos = couTeamMebMapper.getCouTeamMebByTeamId(courseTeamVo.getId());
        courseVo.setCouTeamMeb(couTeamMebVos);
        List<CouRelationVo> subsequents = couRelationMapper.getSubsequent(courseId);
        courseVo.setSubsequents(subsequents);
        List<CouRelationVo> preRepairs = couRelationMapper.getPreRepair(courseId);
        courseVo.setPreRepairs(preRepairs);
        return courseVo;
    }

    @Override
    public void updateCourseInfoByID(CourseInfoDto courseInfoDto) {
        // 执行更新操作
        courseMapper.updateCourseInfoByID(courseInfoDto);
    }


}
